package com.itheima.health.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.upload.UploadUtils;
import com.itheima.health.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("ALL")
/**
 * @Description :套餐管理
 * @author     :LD
 */
@RestController
@RequestMapping("setmeal")
@Api(tags = "预约模块-套餐管理")
public class SetMealController {
    @Reference
    private SetMealService setMealService;

    @PostMapping("upload")
    @ApiOperation(value = "图片上传",notes = "图片上传到oss")
    @ApiImplicitParam(name = "file",value = "文件对象",required = true,dataType = "__file")
    public Result fileupload(@RequestParam("imgFile")MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String uuidFileName = UploadUtils.generateRandonFileName(originalFilename);
        AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidFileName);
        //将上传的图片 保存到集合中
        RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidFileName);
        //图片名保存，过期时间24h
        RedisUtil.set(RedisConstant.SINGLE_PIC+uuidFileName,uuidFileName,20, TimeUnit.SECONDS);
        return new Result(uuidFileName);
    }

    @PostMapping("add")
    @ApiOperation(value = "添加套餐",notes = "添加套餐，传递套餐对象和选中的检查组id")
    @ApiImplicitParam(name = "setmealDto",value = "套餐对象",required = true,dataType = "SetmealDTO")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        setMealService.add(setmealDTO);
        //从redis集合中移除
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        //将未过期的图片移除
        RedisUtil.delete(RedisConstant.SINGLE_PIC+setmealDTO.getImg());
        return new Result(true);
    }

    @PostMapping("findPage")
    @ApiOperation(value = "分页查询",notes = "根据条件分页查询套餐")
    @ApiImplicitParam(name = "queryPageBean",value = "分页参数",required = true,dataType = "QueryPageBean")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Map map = setMealService.findPage(queryPageBean);
        return new Result(map);
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除操作",notes = "根据id删除")
    @ApiImplicitParam(name = "id",value = "套餐id",required = true,dataType = "Integer")
    public Result deleteById(@PathVariable("id")Integer id){
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setIs_delete(1);
        setMealService.updateById(setmeal);
        return new Result(true);
    }

    @GetMapping("findGroupById/{id}")
    @ApiOperation(value = "查询套餐组",notes = "根据id查询套餐绑定的检查组信息")
    @ApiImplicitParam(name = "id",value = "套餐id",required = true,dataType = "Integer")
    public Result findGroupById(@PathVariable("id")Integer id){
        SetmealVO setmealVO = setMealService.findGroupById(id);
        return new Result(setmealVO);
    }
}
